﻿using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Objects;

namespace Fujitsu.JiraAnalysis.DAL
{
    public interface IStatusStorage
    {
        List<StatusMapping> GetStatuses(int projectId);
    }

    public class StatusStorage : IStatusStorage
    {
        public List<StatusMapping> GetStatuses(int projectId)
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                db.Refresh(RefreshMode.StoreWins, db.StatusMapping);
                return (from s in db.StatusMapping
                        where s.ProjectID == projectId
                        select s).ToList();
            }
        }
        public List<Status> GetStatuses()
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                return db.Status.ToList();
            }
        }

        public void UpdateMapping(int projectId, int jiraStatusId, int dbStatusId, string jiraStatusName)
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                var statusMapping = (from status in db.StatusMapping
                                     where status.ProjectID == projectId
                                     && status.JiraStatusID == jiraStatusId
                                     && status.Name == jiraStatusName
                                     select status).FirstOrDefault();

                var isToAddStatusMapping = false;

                if (statusMapping != null)
                {
                    if (statusMapping.StatusID != dbStatusId)
                    {
                        db.StatusMapping.DeleteObject(statusMapping);
                        isToAddStatusMapping = true;
                    }
                }
                else
                {
                    isToAddStatusMapping = true;
                }

                if (isToAddStatusMapping)
                {
                    var statusNewMapping = new StatusMapping
                    {
                        ProjectID = projectId,
                        JiraStatusID = jiraStatusId,
                        StatusID = dbStatusId,
                        Name = jiraStatusName
                    };
                    try
                    {
                        db.StatusMapping.AddObject(statusNewMapping);
                        db.SaveChanges();
                    }
                    catch (OptimisticConcurrencyException)
                    {
                        db.Refresh(RefreshMode.ClientWins, statusNewMapping);
                        db.SaveChanges();
                    }
                    catch (DataException)
                    {
                    }
                }
            }
        }

        public int GetStatusMapping(int projectId, int jiraStatusId)
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                var statusMapping = db.StatusMapping.Where(r => r.ProjectID == projectId && r.JiraStatusID == jiraStatusId).FirstOrDefault();
                if (statusMapping != null)
                    return statusMapping.StatusID;
                return -1;
            }
        }

        public void UpdateStatusName(int projectId, int jiraStatusId, string statusName)
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                var statusMap = db.StatusMapping.Where(
                        s => s.ProjectID == projectId && s.JiraStatusID == jiraStatusId).
                        FirstOrDefault();
                if (statusMap == null)
                    return;
                statusMap.Name = statusName;
                try
                {
                    db.SaveChanges();
                }
                catch (OptimisticConcurrencyException)
                {
                    db.Refresh(RefreshMode.ClientWins, statusMap);
                    db.SaveChanges();
                }
                catch (DataException)
                {
                }
            }
        }

        public List<StatusMapping> GetStatusMappings(int projcetId)
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                return db.StatusMapping.Where(i => i.ProjectID == projcetId).ToList();
                try
                {
                    return db.StatusMapping.Where(i => i.ProjectID == projcetId).ToList();
                }
                catch (EntityException ex)
                {
                    return new List<StatusMapping>();
                }
            }
        }

        public void DeleteStatusMapping(int projectId, int jiraStatusId, int statusId)
        {
            using (var db = new JiraAnalysisDBEntities())
            {
                var dbStatusMapping = db.StatusMapping.FirstOrDefault(i => i.ProjectID == projectId && i.JiraStatusID == jiraStatusId && i.StatusID == statusId);
                if (dbStatusMapping == null)
                    return;

                try
                {
                    db.StatusMapping.DeleteObject(dbStatusMapping);
                    db.SaveChanges();
                }
                catch (OptimisticConcurrencyException)
                {
                    db.Refresh(RefreshMode.ClientWins, dbStatusMapping);
                    db.SaveChanges();
                }
                catch (DataException ex)
                {
                    //EventLogger.CreateInstance(eventLog).LogWriteError(string.Concat("Can't delete issue from DB.", Environment.NewLine, ex.StackTrace));
                }
            }
        }

        public void DeleteStatusMappingsOfProject(int projcetId)
        {
            var statusMappings = GetStatusMappings(projcetId);
            foreach (var statusMapping in statusMappings)
            {
                DeleteStatusMapping(statusMapping.ProjectID, statusMapping.JiraStatusID, statusMapping.StatusID);
            }
        }
    }
}
